package com.unisyou.controller;

import com.unisyou.config.PropertiesConfig;
import com.unisyou.utils.*;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 打卡记录,现金券领取
 */
@RestController
@Slf4j
@RequestMapping("/punch")
public class PunchController {

    @Autowired
    private PropertiesConfig propertiesConfig;
    @Autowired
    private HttpClient httpClient;
    @Autowired
    private WxMpService wxMpService;

    /**
     * 获取用户获奖记录
     * @param request
     * @return
     */
    @RequestMapping("/getPunchRecord")
    public R getPunchRecord(HttpServletRequest request) {
        String url = propertiesConfig.getUrl()+propertiesConfig.getGetPunchRecordPath();
        String userName = CookieUtil.get(request,"mobile").getValue();
        String token = CookieUtil.get(request,"token").getValue();
        Map<String, Object> map = new HashMap<>();
        map.put("userName",userName);
        String paramString = JSONUtils.beanToJson(map);
        Map<String,Object> paramsMap = SignDecode.getInstance().encode(paramString);
        paramsMap.put("token",token);
        Map<String,Object> resultMap = httpClient.clientForPostBody(url,paramsMap);
        if(!"0".equals(resultMap.get("resultCode").toString())){
            log.info("获取打卡记录失败:{}",resultMap.get("resultMsg").toString());
            return R.error();
        }
        List<Map<String, Object>> resultData = JSONUtils.jsonStringToList(resultMap.get("resultData").toString());
        if(resultData.size()>0){
            log.info("获取打卡记录成功,{}",resultData);
            //未领取
            List<Map<String, Object>> unclaimedList = new ArrayList<>();
            //已领取
            List<Map<String, Object>> claimedList = new ArrayList<>();
            //已过期
            List<Map<String, Object>> expiredList = new ArrayList<>();
            for(Map<String, Object> tempMap:resultData){
                if("0".equals(tempMap.get("prize").toString())){
                    unclaimedList.add(tempMap);
                }
                if("1".equals(tempMap.get("prize").toString())){
                    claimedList.add(tempMap);
                }
                if("2".equals(tempMap.get("prize").toString())){
                    expiredList.add(tempMap);
                }
            }
            return R.ok().put("unclaimedData",unclaimedList).put("claimedData",claimedList).put("expiredData",expiredList);
        }
        return R.error();
    }

    /**
     * 修改用户获奖记录(未领取修改为已领取)
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("/updatePunchRecord")
    public R updatePunchRecord(@RequestBody Map<String,Object> map,HttpServletRequest request){
        Map<String, Object> resultMap = null;
        if(map != null && "1".equals(map.get("prize").toString())){
            String url = propertiesConfig.getUrl()+propertiesConfig.getUpdatePunchRecordPath();
            String token = CookieUtil.get(request,"token").getValue();
            String paramString = JSONUtils.beanToJson(map);
            Map<String, Object> encodeMap = SignDecode.getInstance().encode(paramString);
            encodeMap.put("token", token);
            resultMap = httpClient.clientForPostBody(url, encodeMap);
            if("0".equals(resultMap.get("resultCode").toString())){
                log.info("领取成功");
                return R.ok();
            }
        }
        log.info("领取失败:{}",resultMap.get("resultMsg").toString());
        return R.error();
    }
}
